package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import com.mmall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Czh
 * @Date: 2019/6/5  13:24
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/manage/order")
public class OrderManageController {


    @Autowired
    private IUserService iUserService;

    @Autowired
    private IOrderService iOrderService;


    @RequestMapping(value = "list.do",method = RequestMethod.GET)
    public ServerResponse<PageInfo> orderList(HttpServletRequest httpServletRequest, @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum ,
                                              @RequestParam(value = "pageSize" , defaultValue = "10") Integer pageSize){
//        //User user = (User) session.getAttribute(Const.CURRENT_USER);
//        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr,User.class);
//        if (user == null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode() , "用户未登录，请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //是管理员
//            // todo 添加处理逻辑
//            return iOrderService.manageList(pageNum,pageSize);
//        } else {
//            return ServerResponse.createByErrorMessage("无权限，需要管理员权限");
//        }
        //全部通过拦截器验证是否登录以及权限
        return iOrderService.manageList(pageNum,pageSize);


    }

    @RequestMapping(value = "detail.do",method = RequestMethod.POST)
    public ServerResponse<OrderVo> orderList(HttpServletRequest httpServletRequest , Long orderNo){
//        //User user = (User) session.getAttribute(Const.CURRENT_USER);
//        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr,User.class);
//        if (user == null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode() , "用户未登录，请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //是管理员
//            // todo 添加处理逻辑
//            return iOrderService.manageDetail(orderNo);
//        } else {
//            return ServerResponse.createByErrorMessage("无权限，需要管理员权限");
//        }
        //全部通过拦截器验证是否登录以及权限
        return iOrderService.manageDetail(orderNo);

    }


    @RequestMapping(value = "search.do" ,method = RequestMethod.POST)
    public ServerResponse<PageInfo> orderSearch(HttpServletRequest httpServletRequest , Long orderNo , @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum ,
                                               @RequestParam(value = "pageSize" , defaultValue = "10") Integer pageSize){
//        //User user = (User) session.getAttribute(Const.CURRENT_USER);
//        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr,User.class);
//        if (user == null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode() , "用户未登录，请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //是管理员
//            // todo 添加处理逻辑
//            return iOrderService.manageSearch(orderNo,pageNum,pageSize);
//        } else {
//            return ServerResponse.createByErrorMessage("无权限，需要管理员权限");
//        }
        //全部通过拦截器验证是否登录以及权限
        return iOrderService.manageSearch(orderNo,pageNum,pageSize);

    }


    @RequestMapping(value = "send_goods.do" ,method = RequestMethod.POST)
    public ServerResponse<String> OrderSendGoods(HttpServletRequest httpServletRequest , Long orderNo){
//        //User user = (User) session.getAttribute(Const.CURRENT_USER);
//        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr,User.class);
//        if (user == null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode() , "用户未登录，请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //是管理员
//            // todo 添加处理逻辑
//            return iOrderService.manageOrderSendGoods(orderNo);
//        } else {
//            return ServerResponse.createByErrorMessage("无权限，需要管理员权限");
//        }
        //全部通过拦截器验证是否登录以及权限
        return iOrderService.manageOrderSendGoods(orderNo);

    }







}
